import numpy as np
import json
import datetime
import threading

json_string = '[{"id":"17121793","probabilities":[0.046344444,0.046344444,0.046344444,0.046344444,0.046344444,0.046344444,0.046344444,0.046344444,0.046344444,0.040072728,0.040072728,0.040072728,0.040072728,0.040072728,0.040072728,0.040072728,0.040072728,0.040072728,0.040072728,0.040072728]},{"id":"17121774","probabilities":[0.05262222,0.05262222,0.05262222,0.05262222,0.05262222,0.05262222,0.05262222,0.05262222,0.05262222,0.03318182,0.03318182,0.03318182,0.03318182,0.03318182,0.03318182,0.03318182,0.03318182,0.03318182,0.03318182,0.03318182]}]'
final_selected_demand_id = "none"
final_selected_time = 20
is_selected = False;
respond_second_num = 20;

def respond(json_string):
    demands_infos = json.loads(json_string)
    global final_selected_demand_id, final_selected_time

    for demand_info in demands_infos:
         selected_time = round_select(demand_info['probabilities'])
         if selected_time < final_selected_time:
             final_selected_time = selected_time
             final_selected_demand_id = demand_info['id']
    print(final_selected_demand_id + " " + str(final_selected_time))


def round_select(one_to_one_probs):
    select_demand_prob_part = np.random.rand()
    prob_total = 0.0
    prob_index = 0
    selected_time = 20;
    for prob in one_to_one_probs:
        prob_total += prob
        if prob_total > select_demand_prob_part:
            selected_time = prob_index
            break
        prob_index += 1
    return selected_time

for i in range(100):
    final_selected_demand_id = "none"
    final_selected_time = 20
    respond(json_string)

a = 5
if not a is None:
    print("aaaaaaa")